#include "AutomaticXml.h"

AutomaticXml::AutomaticXml()
{
    m_automaticList.clear();
}

bool AutomaticXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "Automatic")   //解析自动化
        {
            AutomaticStruct automatic;

            QDomNode automaticChild = rootChild.toElement().firstChild();
            while(!automaticChild.isNull())
            {
                if(automaticChild.toElement().tagName() == "Name")
                {
                    QString name = automaticChild.toElement().text();
                    automatic.setName(name);
                }
                if(automaticChild.toElement().tagName() == "Id")
                {
                    quint64 id = automaticChild.toElement().text().toUInt();
                    automatic.setId(id);
                }
                if(automaticChild.toElement().tagName() == "IcoId")
                {
                    quint16 icoId = automaticChild.toElement().text().toUInt();
                    automatic.setIcoId(icoId);
                }
                if(automaticChild.toElement().tagName() == "IsExecute")
                {
                    quint16 isExecute = automaticChild.toElement().text().toUInt();
                    automatic.setIsExecute(isExecute);
                }

                if(automaticChild.toElement().tagName() == "ConditionSatisfyType")
                {
                    quint8 conditionSatisfyType = automaticChild.toElement().text().toUInt();
                    automatic.setConditionSatisfyType(conditionSatisfyType);
                }

                if(automaticChild.toElement().tagName() == "ActionList")
                {
                    QDomNode actionListChild = automaticChild.toElement().firstChild();
                    QList<ActionStruct> actionList;
                    while(!actionListChild.isNull())
                    {
                        if (actionListChild.toElement().tagName() == "Action")
                        {
                            ActionStruct action;
                            QDomNode actionChild = actionListChild.toElement().firstChild();
                            while(!actionChild.isNull())
                            {
                                if(actionChild.toElement().tagName() == "ZoneName")
                                {
                                    QString zoneName = actionChild.toElement().text();
                                    action.setZoneName(zoneName);
                                }
                                if(actionChild.toElement().tagName() == "DeviceName")
                                {
                                    QString deviceName = actionChild.toElement().text();
                                    action.setDeviceName(deviceName);
                                }
                                if(actionChild.toElement().tagName() == "FunctionName")
                                {
                                    QString functionName = actionChild.toElement().text();
                                    action.setFunctionName(functionName);
                                }
                                if(actionChild.toElement().tagName() == "ValueName")
                                {
                                    QString valueName = actionChild.toElement().text();
                                    action.setValueName(valueName);
                                }
                                if(actionChild.toElement().tagName() == "ZoneCode")
                                {
                                    quint16 zoneCode = actionChild.toElement().text().toUInt();
                                    action.setZoneCode(zoneCode);
                                }
                                if(actionChild.toElement().tagName() == "DeviceTypeCode")
                                {
                                    quint16 deviceTypeCode = actionChild.toElement().text().toUInt();
                                    action.setDeviceTypeCode(deviceTypeCode);
                                }
                                if(actionChild.toElement().tagName() == "DeviceCode")
                                {
                                    quint16 deviceCode = actionChild.toElement().text().toUInt();
                                    action.setDeviceCode(deviceCode);
                                }
                                if(actionChild.toElement().tagName() == "FunctionCode")
                                {
                                    quint16 functionCode = actionChild.toElement().text().toUInt();
                                    action.setFunctionCode(functionCode);
                                }
                                if(actionChild.toElement().tagName() == "FunctionValue")
                                {
                                    quint16 functionValue = actionChild.toElement().text().toUInt();
                                    action.setFunctionValue(functionValue);
                                }
                                if(actionChild.toElement().tagName() == "Address")
                                {
                                    quint16 address = actionChild.toElement().text().toUInt();
                                    action.setAddress(address);
                                }
                                if(actionChild.toElement().tagName() == "DelayTime")
                                {
                                    quint16 delayTime = actionChild.toElement().text().toUInt();
                                    action.setDelayTime(delayTime);
                                }
                                actionChild = actionChild.nextSibling();
                            }
                            actionList.append(action);

                        }
                        actionListChild = actionListChild.nextSibling();
                    }
                    automatic.setActionList(actionList);
                }

                if(automaticChild.toElement().tagName() == "EquipmentConditionList")
                {
                    QDomNode equipmentConditionListChild = automaticChild.toElement().firstChild();
                    QList<EquipmentConditionStruct> equipmentConditionList;
                    while(!equipmentConditionListChild.isNull())
                    {
                        if (equipmentConditionListChild.toElement().tagName() == "EquipmentCondition")
                        {
                            EquipmentConditionStruct equipmentCondition;
                            QDomNode equipmentConditionChild = equipmentConditionListChild.toElement().firstChild();
                            while(!equipmentConditionChild.isNull())
                            {
                                if(equipmentConditionChild.toElement().tagName() == "ZoneName")
                                {
                                    QString zoneName = equipmentConditionChild.toElement().text();
                                    equipmentCondition.setZoneName(zoneName);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "DeviceName")
                                {
                                    QString deviceName = equipmentConditionChild.toElement().text();
                                    equipmentCondition.setDeviceName(deviceName);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "FunctionName")
                                {
                                    QString functionName = equipmentConditionChild.toElement().text();
                                    equipmentCondition.setFunctionName(functionName);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "ValueName")
                                {
                                    QString valueName = equipmentConditionChild.toElement().text();
                                    equipmentCondition.setValueName(valueName);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "ZoneCode")
                                {
                                    quint16 zoneCode = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setZoneCode(zoneCode);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "DeviceTypeCode")
                                {
                                    quint16 deviceTypeCode = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setDeviceTypeCode(deviceTypeCode);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "DeviceCode")
                                {
                                    quint16 deviceCode = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setDeviceCode(deviceCode);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "FunctionCode")
                                {
                                    quint16 functionCode = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setFunctionCode(functionCode);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "FunctionValue")
                                {
                                    quint16 functionValue = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setFunctionValue(functionValue);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "Address")
                                {
                                    quint16 address = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setAddress(address);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "Number")
                                {
                                    quint16 number = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setNumber(number);
                                }
                                if(equipmentConditionChild.toElement().tagName() == "ConditionType")
                                {
                                    quint16 conditionType = equipmentConditionChild.toElement().text().toUInt();
                                    equipmentCondition.setConditionType(conditionType);
                                }

                                equipmentConditionChild = equipmentConditionChild.nextSibling();
                            }
                            equipmentConditionList.append(equipmentCondition);
                        }
                        equipmentConditionListChild = equipmentConditionListChild.nextSibling();
                    }
                    automatic.setEquipmentConditionList(equipmentConditionList);
                }

                if(automaticChild.toElement().tagName() == "TimeConditionList")
                {
                    QDomNode timeConditionListChild = automaticChild.toElement().firstChild();
                    QList<TimeConditionStruct> timeConditionList;
                    while(!timeConditionListChild.isNull())
                    {
                        if (timeConditionListChild.toElement().tagName() == "TimeCondition")
                        {
                            TimeConditionStruct timeCondition;
                            QDomNode timeConditionChild = timeConditionListChild.toElement().firstChild();
                            while(!timeConditionChild.isNull())
                            {
                                if(timeConditionChild.toElement().tagName() == "Time")
                                {
                                    QString time = timeConditionChild.toElement().text();
                                    timeCondition.setTime(time);
                                }

                                if(timeConditionChild.toElement().tagName() == "Number")
                                {
                                    quint16 number = timeConditionChild.toElement().text().toUInt();
                                    timeCondition.setNumber(number);
                                }

                                timeConditionChild = timeConditionChild.nextSibling();
                            }
                            timeConditionList.append(timeCondition);
                        }
                        timeConditionListChild = timeConditionListChild.nextSibling();
                    }
                    automatic.setTimeConditionList(timeConditionList);
                }
                automaticChild = automaticChild.nextSibling();
            }
            m_automaticList.append(automatic);
        }
        rootChild = rootChild.nextSibling();
    }
    return true;

}

bool AutomaticXml::modify(const AutomaticStruct &automatic)
{
    if(!this->check())
    {
        return false;
    }

    m_automaticList.append(automatic);

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root=doc.documentElement();
    QDomText domText;

    QDomElement automaticElement = doc.createElement("Automatic");

    QDomElement nameElement = doc.createElement("Name");
    QString name = automatic.name();
    domText = doc.createTextNode(name);
    nameElement.appendChild(domText);
    automaticElement.appendChild(nameElement);

    QDomElement idElement = doc.createElement("Id");
    QString id = QString::number(automatic.id(), 10);
    domText = doc.createTextNode(id);
    idElement.appendChild(domText);
    automaticElement.appendChild(idElement);


    QDomElement icoIdElement = doc.createElement("IcoId");
    QString icoId = QString::number(automatic.icoId(), 10);
    domText = doc.createTextNode(icoId);
    icoIdElement.appendChild(domText);
    automaticElement.appendChild(icoIdElement);

    QDomElement isExecuteElement = doc.createElement("IsExecute");
    QString isExecute = QString::number(automatic.isExecute());
    domText = doc.createTextNode(isExecute);
    isExecuteElement.appendChild(domText);
    automaticElement.appendChild(isExecuteElement);

    QDomElement conditionSatisfyTypeElement = doc.createElement("ConditionSatisfyType");
    QString conditionSatisfyType = QString::number(automatic.conditionSatisfyType(),10);
    domText = doc.createTextNode(conditionSatisfyType);
    conditionSatisfyTypeElement.appendChild(domText);
    automaticElement.appendChild(conditionSatisfyTypeElement);



    QDomElement actionListElement = doc.createElement("ActionList");
    for(int i=0; i<automatic.actionList().size(); i++)
    {
        ActionStruct action = automatic.actionList().at(i);
        QDomElement actionElement = doc.createElement("Action");

        QDomElement zoneNameElement = doc.createElement("ZoneName");
        QString zoneName = action.zoneName();
        domText = doc.createTextNode(zoneName);
        zoneNameElement.appendChild(domText);
        actionElement.appendChild(zoneNameElement);

        QDomElement deviceNameElement = doc.createElement("DeviceName");
        QString deviceName =action.deviceName();
        domText = doc.createTextNode(deviceName);
        deviceNameElement.appendChild(domText);
        actionElement.appendChild(deviceNameElement);

        QDomElement functionNameElement = doc.createElement("FunctionName");
        QString functionName = action.functionName();
        domText = doc.createTextNode(functionName);
        functionNameElement.appendChild(domText);
        actionElement.appendChild(functionNameElement);

        QDomElement valueNameElement = doc.createElement("ValueName");
        QString valueName = action.valueName();
        domText = doc.createTextNode(valueName);
        valueNameElement.appendChild(domText);
        actionElement.appendChild(valueNameElement);

        QDomElement zoneCodeElement = doc.createElement("ZoneCode");
        QString zoneCode = QString::number(action.zoneCode(), 10);
        domText = doc.createTextNode(zoneCode);
        zoneCodeElement.appendChild(domText);
        actionElement.appendChild(zoneCodeElement);

        QDomElement deviceTypeCodeElement = doc.createElement("DeviceTypeCode");
        QString deviceTypeCode = QString::number(action.deviceTypeCode(), 10);
        domText = doc.createTextNode(deviceTypeCode);
        deviceTypeCodeElement.appendChild(domText);
        actionElement.appendChild(deviceTypeCodeElement);

        QDomElement deviceCodeElement = doc.createElement("DeviceCode");
        QString deviceCode = QString::number(action.deviceCode(), 10);
        domText = doc.createTextNode(deviceCode);
        deviceCodeElement.appendChild(domText);
        actionElement.appendChild(deviceCodeElement);

        QDomElement functionCodeElement = doc.createElement("FunctionCode");
        QString functionCode = QString::number(action.functionCode(), 10);
        domText = doc.createTextNode(functionCode);
        functionCodeElement.appendChild(domText);
        actionElement.appendChild(functionCodeElement);

        QDomElement functionValueElement = doc.createElement("FunctionValue");
        QString functionValue = QString::number(action.functionValue(), 10);
        domText = doc.createTextNode(functionValue);
        functionValueElement.appendChild(domText);
        actionElement.appendChild(functionValueElement);

        QDomElement addressElement = doc.createElement("Address");
        QString address = QString::number(action.address(), 10);
        domText = doc.createTextNode(address);
        addressElement.appendChild(domText);
        actionElement.appendChild(addressElement);

        QDomElement delayTimeElement = doc.createElement("DelayTime");
        QString delayTime = QString::number(action.delayTime(), 10);
        domText = doc.createTextNode(delayTime);
        delayTimeElement.appendChild(domText);
        actionElement.appendChild(delayTimeElement);

        actionListElement.appendChild(actionElement);
    }
    automaticElement.appendChild(actionListElement);

    QDomElement equipmentConditionListElement = doc.createElement("EquipmentConditionList");
    for(int i=0; i<automatic.equipmentConditionList().size(); i++)
    {
        EquipmentConditionStruct equipmentCondition = automatic.equipmentConditionList().at(i);
        QDomElement equipmentConditionElement = doc.createElement("EquipmentCondition");

        QDomElement zoneNameElement = doc.createElement("ZoneName");
        QString zoneName = equipmentCondition.zoneName();
        domText = doc.createTextNode(zoneName);
        zoneNameElement.appendChild(domText);
        equipmentConditionElement.appendChild(zoneNameElement);

        QDomElement deviceNameElement = doc.createElement("DeviceName");
        QString deviceName = equipmentCondition.deviceName();
        domText = doc.createTextNode(deviceName);
        deviceNameElement.appendChild(domText);
        equipmentConditionElement.appendChild(deviceNameElement);

        QDomElement functionNameElement = doc.createElement("FunctionName");
        QString functionName = equipmentCondition.functionName();
        domText = doc.createTextNode(functionName);
        functionNameElement.appendChild(domText);
        equipmentConditionElement.appendChild(functionNameElement);

        QDomElement valueNameElement = doc.createElement("ValueName");
        QString valueName = equipmentCondition.valueName();
        domText = doc.createTextNode(valueName);
        valueNameElement.appendChild(domText);
        equipmentConditionElement.appendChild(valueNameElement);

        QDomElement zoneCodeElement = doc.createElement("ZoneCode");
        QString zoneCode = QString::number(equipmentCondition.zoneCode(), 10);
        domText = doc.createTextNode(zoneCode);
        zoneCodeElement.appendChild(domText);
        equipmentConditionElement.appendChild(zoneCodeElement);

        QDomElement deviceTypeCodeElement = doc.createElement("DeviceTypeCode");
        QString deviceTypeCode = QString::number(equipmentCondition.deviceTypeCode(), 10);
        domText = doc.createTextNode(deviceTypeCode);
        deviceTypeCodeElement.appendChild(domText);
        equipmentConditionElement.appendChild(deviceTypeCodeElement);

        QDomElement deviceCodeElement = doc.createElement("DeviceCode");
        QString deviceCode = QString::number(equipmentCondition.deviceCode(), 10);
        domText = doc.createTextNode(deviceCode);
        deviceCodeElement.appendChild(domText);
        equipmentConditionElement.appendChild(deviceCodeElement);

        QDomElement functionCodeElement = doc.createElement("FunctionCode");
        QString functionCode = QString::number(equipmentCondition.functionCode(), 10);
        domText = doc.createTextNode(functionCode);
        functionCodeElement.appendChild(domText);
        equipmentConditionElement.appendChild(functionCodeElement);

        QDomElement functionValueElement = doc.createElement("FunctionValue");
        QString functionValue = QString::number(equipmentCondition.functionValue(), 10);
        domText = doc.createTextNode(functionValue);
        functionValueElement.appendChild(domText);
        equipmentConditionElement.appendChild(functionValueElement);

        QDomElement addressElement = doc.createElement("Address");
        QString address = QString::number(equipmentCondition.address(), 10);
        domText = doc.createTextNode(address);
        addressElement.appendChild(domText);
        equipmentConditionElement.appendChild(addressElement);

        QDomElement numberElement = doc.createElement("Number");
        QString number = QString::number(equipmentCondition.number(), 10);
        domText = doc.createTextNode(number);
        numberElement.appendChild(domText);
        equipmentConditionElement.appendChild(numberElement);

        QDomElement conditionTypeElement = doc.createElement("ConditionType");
        QString conditionType = QString::number(equipmentCondition.conditionType(), 10);
        domText = doc.createTextNode(conditionType);
        conditionTypeElement.appendChild(domText);
        equipmentConditionElement.appendChild(conditionTypeElement);

        equipmentConditionListElement.appendChild(equipmentConditionElement);
    }
    automaticElement.appendChild(equipmentConditionListElement);
    printf("create timeConditionListElement- %d\r\n",automatic. timeConditionList().size());
    QDomElement timeConditionListElement = doc.createElement("TimeConditionList");
    for(int i=0; i<automatic. timeConditionList().size(); i++)
    {
        TimeConditionStruct timeCondition = automatic.timeConditionList().at(i);
        QDomElement timeConditionElement = doc.createElement("TimeCondition");

        QDomElement timeElement = doc.createElement("Time");
        QString time = timeCondition.time();
        domText = doc.createTextNode(time);
        timeElement.appendChild(domText);
        timeConditionElement.appendChild(timeElement);

        QDomElement numberElement = doc.createElement("Number");
        QString number = QString::number(timeCondition.number(), 10);
        domText = doc.createTextNode(number);
        numberElement.appendChild(domText);
        timeConditionElement.appendChild(numberElement);

        timeConditionListElement.appendChild(timeConditionElement);
    }
    automaticElement.appendChild(timeConditionListElement);

    root.appendChild(automaticElement);

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    return true;


}

bool AutomaticXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_automaticList.clear();
    return true;
}

QList<AutomaticStruct> AutomaticXml::automaticList() const
{
    return m_automaticList;
}

void AutomaticXml::setAutomaticList(const QList<AutomaticStruct> &automaticList)
{
    m_automaticList = automaticList;
}



quint16 AutomaticXml::getTimetoGO()
{
    return m_Timetogo;
}
bool AutomaticXml::getTestStata()
{
    return m_teststata;
}
void AutomaticXml::setTimetoGO(quint16 time)
{
    m_Timetogo=time;
}
void AutomaticXml::setTestStata(bool stata)
{
    m_teststata=stata;
}

quint16 AutomaticXml::getBestTime()
{
    return m_BestTime;
}
bool AutomaticXml::getBestStata()
{
    return m_BestStata;
}
void AutomaticXml::setBestTime(quint16 time)
{
    m_BestTime=time;
}
void AutomaticXml::setBestStata(bool stata)
{
    m_BestStata=stata;
}




